package com.woniuxy.book.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.book.controller.form.orderrefund.ApplyNoPassForm;
import com.woniuxy.book.controller.form.orderrefund.ApplyRefundForm;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.param.orderrefund.ApplyNoPassParam;
import com.woniuxy.book.param.orderrefund.ApplyRefundParam;
import com.woniuxy.book.param.orderrefund.RefundForm;
import com.woniuxy.book.param.orderrefund.RefundParam;
import com.woniuxy.book.service.OrderRefundService;
import com.woniuxy.book.util.JWTUtil;
import com.woniuxy.book.util.RedisUtil;
import com.woniuxy.book.util.RequiredRole;
import com.woniuxy.book.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@RestController
@RequestMapping("/orderRefund")
public class OrderRefundController {

    @Resource
    private OrderRefundService orderRefundService;
    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/user/applyRefund")
    public Result applyRefund(@RequestBody ApplyRefundForm form, @RequestHeader("token")String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        ApplyRefundParam param = BeanUtil.toBean(form, ApplyRefundParam.class);
        param.setUserId(userId);
        orderRefundService.applyRefund(param);
        return Result.success("退款申请成功",null);
    }

    @PostMapping("/admin/refund")
    @RequiredRole(value = "管理员")
    public Result refund(@RequestBody RefundForm form){
        RefundParam param = BeanUtil.toBean(form, RefundParam.class);
        orderRefundService.refund(param);
        return Result.success("退款成功",null);
    }

    @GetMapping("/admin/selectRefundOrders")
    public Result selectRefundOrders(Integer pageNum, Integer pageSize, Integer refundState){
        PageInfo pageInfo = orderRefundService.selectRefundOrders(pageNum,pageSize,refundState);
        return Result.success("查询所有退款订单成功",pageInfo);
    }

    @GetMapping("/user/searchRefundOrder")
    public Result searchRefundOrder(Integer pageNum,Integer pageSize,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        PageInfo pageInfo = orderRefundService.searchRefundOrder(pageNum,pageSize,userId);
        return Result.success("查询退款订单成功",pageInfo);
    }


    @PostMapping("/admin/applyNoPass")
    @RequiredRole(value = "管理员")
    public Result applyNoPass(@RequestBody ApplyNoPassForm form){
        orderRefundService.applyNoPass(BeanUtil.toBean(form, ApplyNoPassParam.class));
        return Result.success("驳回退款申请成功",null);
    }

    @GetMapping("/user/applyRefundAgain")
    public Result applyRefundAgain(String refundId,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        orderRefundService.applyRefundAgain(refundId,userId);
        return Result.success("已重新申请退款",null);
    }




}

